<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * Created by PhpStorm.
 * Author: netman
 * QQ: 1649513971
 * Authorize:http://www.stourweb.com
 * Time: 2019/2/26 17:32
 * Desc:
 */
class Model_Provider_Api_Tools
{
    /*
       * 将相对文件url换成绝对文件url
       */
    public static function get_resource_url($url)
    {

        if (!empty($url)) {
            $http = stripos(strtolower($url), 'http://');
            $https = stripos(strtolower($url), 'https://');
            if ($http === $https) {
                $web_url = self::get_web_url();
                return $web_url . $url;
            }
        }
        return $url;
    }

    function filter_content($content)
    {
        $preg = '/<img.*?src=[\"|\'](.*?)[\"|\']?\s(.*?)>/is';

        $content = preg_replace_callback(
            $preg,
            function ($matches) {
                $src = $matches[1];
                if (strpos($src, 'http') === false) {
                    $src = self::get_resource_url($src);
                }
                return '<img src="' . $src . '" ' . $matches[2] . '>';
            },
            $content
        );
        return $content;


    }

    /**
     * 产品图片
     * @param $picStr
     * @return array
     */
    public static function pic_list($pics)
    {
        if (empty($pics)) {
            return;
        }
        $arr = explode(',', $pics);
        foreach ($arr as &$v) {
            $v = explode('||', $v);
        }
        return $arr;
    }

    public static function get_web_url()
    {
        $row = DB::select('weburl')->from('weblist')->where('webid', '=', 0)->execute()->current();
        return $row['weburl'];
    }

    //检查手机号
    public static function is_mobile($phone)
    {
        $check = '/^(1(([356789][0-9])|(47)))\d{8}$/';
        if (preg_match($check, $phone)) {
            return true;
        } else {
            return false;
        }
    }

    //检查身份证
    public static function is_id_card($card)
    {
        $id = strtoupper($card);
        $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
        $arr_split = array();
        if (!preg_match($regx, $id)) {
            return FALSE;
        }
        if (15 == strlen($id)) //检查15位
        {
            $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";

            @preg_match($regx, $id, $arr_split);
            //检查生日日期是否正确
            $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
            if (!strtotime($dtm_birth)) {
                return FALSE;
            } else {
                return TRUE;
            }
        } else      //检查18位
        {
            $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
            @preg_match($regx, $id, $arr_split);
            $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
            if (!strtotime($dtm_birth)) //检查生日日期是否正确
            {
                return FALSE;
            } else {
                //检验18位身份证的校验码是否正确。
                //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
                $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                $sign = 0;
                for ($i = 0; $i < 17; $i++) {
                    $b = (int)$id{$i};
                    $w = $arr_int[$i];
                    $sign += $b * $w;
                }
                $n = $sign % 11;
                $val_num = $arr_ch[$n];
                if ($val_num != substr($id, 17, 1)) {
                    return FALSE;
                } else {
                    return TRUE;
                }
            }
        }
    }

    /**
     * 检测游客手机
     * @param $tourer_mobile
     * @param $tourer_cardno
     * @return array
     */
    public static function check_tourer_mobile($tourer_info)
    {
        $flag = true;
        foreach ($tourer_info as $t) {
            if ($t['mobile'] && !Model_Zhilvyun_Api_Tools::is_mobile($t['mobile'])) {
                $flag = false;
                $data = $t['mobile'];
                break;
            }
        }
        return array('status' => $flag, 'data' => $data);

    }

    /**
     * 检测游客证件号
     * @param $tourer_mobile
     * @param $tourer_cardno
     * @return array
     */
    public static function check_tourer_cardno($tourer_info)
    {
        $credentials = json_decode($tourer_info['visitors']['credentials'], true);
        $flag = true;
        foreach ($tourer_info as $t) {
            if ($credentials[0] && !Model_Zhilvyun_Api_Tools::is_id_card($credentials[0])) {
                $flag = false;
                $data = $t['IDCard'];
                break;
            }
        }
        return array('status' => $flag, 'data' => $data);;

    }

    /**
     * 相差天数
     * @param $day1
     * @param $day2
     * @return float|int
     */
    public static function diff_between_two_days($day1, $day2)
    {
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);

        if ($second1 < $second2) {
            $tmp = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        return ($second1 - $second2) / 86400;
    }

    /**
     * 获取产品满意度
     * @return int
     */
    public static function get_virtual_satisfy_score()
    {
        return rand(90, 99);
    }

    /**
     * 产品预定数量
     * @return int
     */
    public static function get_book_count()
    {
        return rand(10, 100);
    }

    /**
     * 产品查看数量
     * @return int
     */
    public static function get_show_num()
    {
        return rand(100, 500);
    }
    /*
     * 高德坐标转百度（传入经度、纬度），得到百度的经纬度
     * @param gdlng  高德经度
     * @param gdlat  高德纬度
     */
    public static function coordinate_gd_to_bd($gdlng, $gdlat)
    {
        $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        $x = $gdlng;
        $y = $gdlat;
        $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $x_pi);
        $theta = atan2($y, $x) + 0.000003 * cos($x * $x_pi);
        $data['lng'] = $z * cos($theta) + 0.0065;
        $data['lat'] = $z * sin($theta) + 0.006;
        return $data;
    }

}